<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use app\service\communityService;
use app\service\propertyService;
use app\service\roominfoService;
use think\Db;
class Costtype extends Common
{
    public function costtypeList(){
        //查询服务商数据条数
        $com = $this->pidTokenId();
        $where['id'] = array('in',$com['property_id']);

        //查询服务商信息
        $data = request()->get();
        if (!empty($data)){
            if(!empty($data['property_name'])){
                $where['name'] = array("like","%".$data['property_name']."%");
            }
        }
        $where = array_filter($where);
        $propertylist = Db::name('property')->where($where)->select();
        //var_dump($propertylist);
        $this->assign("propertylist",$propertylist);
        $data = request()->get();
        
        if (!empty($data)){
            $where=array();
            if(!empty($data['property_id'])){
                $where['property_id'] = $data['property_id'];//物业名称
            }else{
                $where['property_id'] = "";
            }
            $this->assign('where',$where);   
            $where = array_filter($where);
        }else{
            $where=array();
        }
        
        $list = (new propertyService())->costListWhere($where,$com['property_id']);
        $this->assign('list',$list);
        //查询费用类型数据条数
        $number = (new propertyService())->costNumberWhere($where,$com['property_id']);
        
        $this->assign('number',$number);
        foreach ($data as $k => $v) {
            $this->assign($k, $v);
        }

        return $this->fetch('costtype_list');
    }
    public function costtypeAdd(){
        $com = $this->pidTokenId();
        $where['id'] = array('in',$com['property_id']);
        //var_dump($com['property_id']);
        $propertylist = Db::name('property')->where($where)->order('id asc')->select();
        $this->assign("propertylist",$propertylist);
        //echo "<pre>";
        //var_dump($propertylist);
        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();
            //判断账号名称是否重复        
            $info = (new propertyService())->costName($data['type'],$data['property_id']);
            if($info){
                $this->error('费用类型已存在');
            }
            $data['time'] = date('Y-m-d H:i:s',time());
            $property=Db::table("property")->where("id",$data["property_id"])->find();
            $data["property_name"]=$property["name"];
            $data['time'] = date('Y-m-d H:i:s',time());
            //插入数据到数据库
            $result = (new propertyService())->costtypeAdd($data);
            if($result){
                return "添加费用类型成功";
                $this->success('添加费用类型成功',url('costtypeList'));
            }else{
                $this->error('添加费用类型失败',url('costtypeAdd'));
            }             
        }
        return $this->fetch('costtype_add');
    }
    public function costtypeEdit(){
        if(request()->isGet()){
            $id = request()->param('id');
            if(!isset($id) && empty($id)){
                $this->error('费用类型不存在');
            }
            $info = (new communityService())->costtypeId($id);
            $this->assign('info',$info);
            return $this->fetch('costtype_edit');
        }else{
            $data = request()->post();
            $result = (new communityService())->costtypeUpdate($data);
            if($result){
                return "修改成功";
                $this->success('修改成功',url('costtypeList'));
            }else{
                $this->error('修改失败');
            }
        }
    }
    public function costtypeDel(){
        $id = request()->param('id');
        //判断是否存在该数据
		$info = (new communityService())->costtypeId($id);
		if(empty($info)){
			$this->error('该费用类型不存在');
		}
		$result = Db::name('costtype')->delete($id);
        if($result){
            $this->success('删除成功',url('costtypeList'));
		}else{
            $this->success('删除失败',url('costtypeList'));
		}
    }
}